#include "stdafx.h"

#ifdef MSCODE

#define xxx_CloseHandle CloseHandle

// from apic.c
static CONST WCHAR szHex[] = L"0123456789ABCDEF";

extern "C" 
UINT core200_GetTempFileNameW(LPCWSTR lpPathName, LPCWSTR lpPrefixString, UINT uUnique, LPWSTR lpTempFileName) {

	OutputDebugString(_T("GetTempFileNameW\n")); 

	DWORD Length, Length2, PassCount, dwAttr;
	UINT uMyUnique;
	HANDLE hFile;
	Length = wcslen(lpPathName);
	if (!Length || (Length >= MAX_PATH)) {
		SetLastError(ERROR_INVALID_PARAMETER);
		return 0;
	}
	memcpy(lpTempFileName,lpPathName,Length*sizeof(WCHAR));
    if (lpTempFileName[Length-1] != (WCHAR)'\\')
        Length++;
    lpTempFileName[Length-1] = 0;
    dwAttr = GetFileAttributesW(lpTempFileName);
    if ((dwAttr == 0xFFFFFFFF) || !(dwAttr & FILE_ATTRIBUTE_DIRECTORY)) {
        SetLastError(ERROR_DIRECTORY);
        return 0;
    }
    lpTempFileName[Length-1] = L'\\';
    PassCount = 0;
    Length2 = wcslen(lpPrefixString);
    if (Length2 > 3)
    	Length2 = 3;
    memcpy(&lpTempFileName[Length],lpPrefixString,Length2*sizeof(WCHAR));
    Length += Length2;
    uUnique &= 0x0000ffff;
    if ((Length + 9) > MAX_PATH) { // 4 hex digits, .tmp,  and a null
		SetLastError(ERROR_INVALID_PARAMETER);
		return 0;
    }
    lpTempFileName[Length+4] = '.';
    lpTempFileName[Length+5] = 't';
    lpTempFileName[Length+6] = 'm';
    lpTempFileName[Length+7] = 'p';
try_again:
    if (!uUnique) {
        if (!(uMyUnique = (UINT)Random() & 0x0000ffff)) {
            if (!(++PassCount & 0xffff0000))
   	        	goto try_again;
   	        SetLastError(ERROR_RETRY);
   	        return 0;
        }
    } else
        uMyUnique = uUnique;
    lpTempFileName[Length] = szHex[(uMyUnique >> 12) & 0xf];
    lpTempFileName[Length+1] = szHex[(uMyUnique >> 8) & 0xf];
    lpTempFileName[Length+2] = szHex[(uMyUnique >> 4) & 0xf];
    lpTempFileName[Length+3] = szHex[uMyUnique & 0xf];
    lpTempFileName[Length+8] = 0;
    if (!uUnique) {
        if ((hFile = CreateFileW(lpTempFileName, GENERIC_READ, 0, 0, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0)) == INVALID_HANDLE_VALUE) {
        	switch (GetLastError()) {
        		case ERROR_FILE_EXISTS:
	        	case ERROR_ALREADY_EXISTS:
		            if (!(++PassCount & 0xffff0000))
	    	        	goto try_again;
	    	    	break;
	    	}
	        return 0;
        } else
         	xxx_CloseHandle(hFile);
    }
    return uMyUnique;
}

#if _WIN32_WCE<420
typedef enum _GET_FILEEX_INFO_LEVELS {
    GetFileExInfoStandard,
    GetFileExMaxInfoLevel
} GET_FILEEX_INFO_LEVELS;

typedef struct _WIN32_FILE_ATTRIBUTE_DATA {
    DWORD dwFileAttributes;
    FILETIME ftCreationTime;
    FILETIME ftLastAccessTime;
    FILETIME ftLastWriteTime;
    DWORD nFileSizeHigh;
    DWORD nFileSizeLow;
} WIN32_FILE_ATTRIBUTE_DATA, *LPWIN32_FILE_ATTRIBUTE_DATA;
#endif

extern "C" 
BOOL core200_GetFileAttributesExW(LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId, LPVOID lpFileInformation) {

	OutputDebugString(_T("GetFileAttributesExW\n")); 

	LPCWSTR pTrav;
	HANDLE hFind;
	WIN32_FIND_DATA w32fd;
	for (pTrav = lpFileName; *pTrav; pTrav++)
		if (*pTrav == '*' || *pTrav == '?') {
			SetLastError(ERROR_INVALID_NAME);
			return FALSE;
		}
	if (fInfoLevelId != GetFileExInfoStandard) {
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}
	if ((hFind = FindFirstFile(lpFileName,&w32fd)) == INVALID_HANDLE_VALUE) {
		SetLastError(ERROR_FILE_NOT_FOUND);
		return FALSE;
	}
	xxx_CloseHandle(hFind);
	((WIN32_FILE_ATTRIBUTE_DATA *)lpFileInformation)->dwFileAttributes = w32fd.dwFileAttributes;
	((WIN32_FILE_ATTRIBUTE_DATA *)lpFileInformation)->ftCreationTime = w32fd.ftCreationTime;
	((WIN32_FILE_ATTRIBUTE_DATA *)lpFileInformation)->ftLastAccessTime = w32fd.ftLastAccessTime;
	((WIN32_FILE_ATTRIBUTE_DATA *)lpFileInformation)->ftLastWriteTime = w32fd.ftLastWriteTime;
	((WIN32_FILE_ATTRIBUTE_DATA *)lpFileInformation)->nFileSizeHigh = w32fd.nFileSizeHigh;
	((WIN32_FILE_ATTRIBUTE_DATA *)lpFileInformation)->nFileSizeLow = w32fd.nFileSizeLow;
	return TRUE;
}

#define _tolower towlower

extern "C" 
int core200__stricmp(const char *str1, const char *str2) {

#ifdef DEBUG
	TCHAR buf[100];
	swprintf( buf, _T("_stricmp: %S, %S\n"), str1, str2 );
	OutputDebugString(buf); 
#endif

	unsigned char ch1, ch2;
    for (;*str1 && *str2; str1++, str2++) {
	ch1 = _tolower(*str1); 
	ch2 = _tolower(*str2);
	if (ch1 != ch2)
		return ch1 - ch2;
    }
    // Check last character.
    return (unsigned char)_tolower(*str1) - (unsigned char)_tolower(*str2);
}

extern "C" 
char * core200_strrchr(const char * string, int ch) {

	OutputDebugString(_T("strrchr\n")); 

	char *start = (char *)string;

	/* find end of string */
	while (*string++);

	/* search towards front */
	while (--string != start && *string != (char) ch);   

	if (*string == (char) ch)		/* char found ? */
		return( (char *)string );
	return(NULL);
}

#else // not MSCODE

extern "C" int core200__stricmp( const char *string1, const char *string2 )
{
	OutputDebugString(_T("_stricmp: "));

	TCHAR strW1[301], strW2[301];
	mbstowcs( strW1, string1, 300 );
	mbstowcs( strW2, string2, 300 );

	OutputDebugString(strW1);
	OutputDebugString(_T(" and "));
	OutputDebugString(strW2);
	OutputDebugString(_T("\n"));

	return wcsicmp( strW1, strW2 );
}

extern "C" char* core200_strrchr( const char *string, int c )
{ 
	OutputDebugString(_T("strrchr: "));

	TCHAR strW[301]; 
	mbstowcs( strW, string, 300 );

	OutputDebugString(strW);
	OutputDebugString(_T("\n"));

	TCHAR* pChW = wcsrchr( strW, c );

	int i = pChW - &strW[0];

	return (char*) &string[i];
}

#endif // MSCODE
